package com.castle.fortress.admin.jmreport.rest;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.castle.fortress.admin.core.constants.GlobalConstants;
import com.castle.fortress.admin.jmreport.entity.JimuReportEntity;
import com.castle.fortress.admin.jmreport.dto.JimuReportDto;
import com.castle.fortress.admin.jmreport.service.CastleJimuReportService;
import com.castle.fortress.common.utils.ConvertUtil;
import com.castle.fortress.common.entity.RespBody;
import com.castle.fortress.common.exception.BizException;
import com.castle.fortress.common.respcode.GlobalRespCode;
import com.castle.fortress.common.enums.OperationTypeEnum;
import com.castle.fortress.admin.core.annotation.CastleLog;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 在线excel设计器 api 控制器
 *
 * @author castle
 * @since 2023-10-11
 */
@Api(tags="在线excel设计器api管理控制器")
@RestController
@RequestMapping("/api/jmreport/jimuReport")
public class ApiJimuReportController {
    @Autowired
    private CastleJimuReportService castleJimuReportService;


    /**
     * 在线excel设计器的分页展示
     * @param jimuReportDto 在线excel设计器实体类
     * @param currentPage 当前页
     * @param size  每页记录数
     * @return
     */
    @CastleLog(operLocation="在线excel设计器-api",operType = OperationTypeEnum.QUERY)
    @ApiOperation("在线excel设计器分页展示")
    @GetMapping("/page")
    public RespBody<IPage<JimuReportDto>> pageJimuReport(JimuReportDto jimuReportDto, @RequestParam(required = false) Integer currentPage, @RequestParam(required = false)Integer size){
        Integer pageIndex= currentPage==null? GlobalConstants.DEFAULT_PAGE_INDEX:currentPage;
        Integer pageSize= size==null? GlobalConstants.DEFAULT_PAGE_SIZE:size;
        Page<JimuReportDto> page = new Page(pageIndex, pageSize);

        IPage<JimuReportDto> pages = castleJimuReportService.pageJimuReport(page, jimuReportDto,null);
        return RespBody.data(pages);
    }

    /**
     * 在线excel设计器保存
     * @param jimuReportDto 在线excel设计器实体类
     * @return
     */
    @CastleLog(operLocation="在线excel设计器-api",operType = OperationTypeEnum.INSERT)
    @ApiOperation("在线excel设计器保存")
    @PostMapping("/save")
    public RespBody<String> saveJimuReport(@RequestBody JimuReportDto jimuReportDto){
        if(jimuReportDto == null ){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
            JimuReportEntity jimuReportEntity = ConvertUtil.transformObj(jimuReportDto,JimuReportEntity.class);
        if(castleJimuReportService.save(jimuReportEntity)){
            return RespBody.data("保存成功");
        }else{
            return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
        }
    }

    /**
     * 在线excel设计器编辑
     * @param jimuReportDto 在线excel设计器实体类
     * @return
     */
    @CastleLog(operLocation="在线excel设计器-api",operType = OperationTypeEnum.UPDATE)
    @ApiOperation("在线excel设计器编辑")
    @PostMapping("/edit")
    public RespBody<String> updateJimuReport(@RequestBody JimuReportDto jimuReportDto){
        if(jimuReportDto == null || jimuReportDto.getId() == null || jimuReportDto.getId().equals(0L)){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
            JimuReportEntity jimuReportEntity = ConvertUtil.transformObj(jimuReportDto,JimuReportEntity.class);
        if(castleJimuReportService.updateById(jimuReportEntity)){
            return RespBody.data("保存成功");
        }else{
            return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
        }
    }

    /**
     * 在线excel设计器删除
     * @param ids 在线excel设计器id集合
     * @return
     */
    @CastleLog(operLocation="在线excel设计器-api",operType = OperationTypeEnum.DELETE)
    @ApiOperation("在线excel设计器删除")
    @PostMapping("/delete")
    public RespBody<String> deleteJimuReport(@RequestBody List<Long> ids){
        if(ids == null || ids.isEmpty()){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
        if(castleJimuReportService.removeByIds(ids)) {
            return RespBody.data("保存成功");
        }else{
            return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
        }
    }

    /**
     * 在线excel设计器详情
     * @param id 在线excel设计器id
     * @return
     */
    @CastleLog(operLocation="在线excel设计器-api",operType = OperationTypeEnum.QUERY)
    @ApiOperation("在线excel设计器详情")
    @GetMapping("/info")
    public RespBody<JimuReportDto> infoJimuReport(@RequestParam Long id){
        if(id == null){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
            JimuReportEntity jimuReportEntity = castleJimuReportService.getById(id);
            JimuReportDto jimuReportDto = ConvertUtil.transformObj(jimuReportEntity,JimuReportDto.class);
        return RespBody.data(jimuReportDto);
    }


}
